package com.example.hanfu_bk.controller;

import com.example.hanfu_bk.pojo.Hanfu;
import com.example.hanfu_bk.pojo.Result;
import com.example.hanfu_bk.service.HanfuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/hanfu")  // 建议单独用/hanfu路径，区分资源
public class HanfuController {

    @Autowired
    private HanfuService hanfuService;

    // 保存汉服定制信息
    @PostMapping("/save")
    public Result saveHanfu(@RequestBody Hanfu hanfu) {
        int rows = hanfuService.saveHanfu(hanfu);
        if (rows > 0) {
            return Result.success("保存成功");  // 返回生成的主键ID
        } else {
            return Result.error("保存失败");
        }
    }
//    查询用户的定制记录
    @GetMapping("/records")
    public Result getRecords(@RequestParam Integer userId) {
        List<Hanfu> records = hanfuService.getRecordsByUserId(userId);
        return Result.success(records);
    }
    @DeleteMapping("/delete")
    public Result deleteRecord(
            @RequestParam Integer userId,
            @RequestParam Integer recordId
    ) {
        try {
            boolean isDeleted = hanfuService.deleteByUserIdAndRecordId(userId, recordId);
            if (isDeleted) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败，记录不存在或无权限");
            }
        } catch (Exception e) {
            return Result.error("删除失败：" + e.getMessage());
        }
    }
}